/*  Ajax.Menu JavaScript framework, version 1.0
 *  Copyright (c) 2005, Glenn Nilsson <glenn.nilsson@gmail.com>
 *
 *  Ajax.Menu is distributed under the terms of an Creative Commons
 *  Attribution license. In short words, you can use this however you like
 *  as long as you give me and the code credit. Read more at:
 *  http://creativecommons.org/licenses/by/2.5/
 *
 *  Requirements: Prototype framework <http://prototype.conio.net/>
 *
 *  For details, see: http://wailqill.com/code/ajax-menu/
 *
/*--------------------------------------------------------------------------*/

JMCMenu = Class.create();
Object.extend(Object.extend(JMCMenu.prototype), 
{
	initialize: function(id) 
	{
		this.id = id;
		this.addObservers();
	},
	addObservers: function() 
	{
		var menu = $(this.id);
		if (menu) 
		{
			var lis = menu.getElementsByTagName("li");
			for (var i=0; lis[i]!= null; i++) 
			{
			    var li = lis[i];
			    if (Element.hasClassName(li,"collapsed")  || Element.hasClassName(li,"expanded"))
			    {
    				Event.observe(li, "click", this.onClick.bindAsEventListener(this));
    			}
			}
		}
	},
	onClick: function( event ) 
	{
		var li = Event.findElement(event, 'li');
		if (li.hasClassName("collapsed"))
	    {
			Event.stop(event);

			var ul = null;
			for (var i=0; node=li.childNodes[i]; i++) 
			{
				if (node.nodeName.toLowerCase() == "ul") 
				{
					ul = node;
					break;
				}
			}
			if (ul) 
			{
//					ul.style.display = "block";
                    ul.style.left = "auto";
				li.removeClassName('collapsed');
				li.addClassName('expanded');
			} 
		}
		else if (li.hasClassName("expanded")) 
		{
			Event.stop(event);

			var uls = li.getElementsByTagName("ul");
			for (var i=0; ul=uls[i]; i++) 
			{
//					ul.style.display = "none";
                    ul.style.left = "-100em";
                ul.parentNode.removeClassName('expanded');
				ul.parentNode.addClassName('collapsed');
			}
            li.removeClassName('expanded');
			li.addClassName('collapsed');
		}
	}
});
